﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Xpo;
using BusinessObjects;


namespace UI
{
    public partial class frmQuanHuyen : FormBase
    {
        public frmQuanHuyen()
        {
            InitializeComponent();
        }

        private void gvQuanHuyen_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                Edit();
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void Edit()
        {
            if (gvQuanHuyen.GetFocusedRow()!= null)
            {
                using (NestedUnitOfWork nuow = xpCollectionQuanHuyen.Session.BeginNestedUnitOfWork())
                {
                    var obj = nuow.GetNestedObject(xpCollectionQuanHuyen[gvQuanHuyen.GetDataSourceRowIndex(gvQuanHuyen.FocusedRowHandle)]) as QuanHuyen;
                    frmQuanHuyen_Edit frm = new frmQuanHuyen_Edit();
                    frm.ItemEdit = obj;
                    frm.TinhThanhID = obj.TinhThanh.Oid;
                    if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        obj.TinhThanh = nuow.Query<TinhThanh>().FirstOrDefault(c => c.Oid == frm.TinhThanhID);
                       
                        nuow.CommitChanges();

                    }
                }
            }
        }

        private void btnThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                using (NestedUnitOfWork nuow = xpCollectionQuanHuyen.Session.BeginNestedUnitOfWork())
                {
                    frmQuanHuyen_Edit frm = new frmQuanHuyen_Edit();
                    var obj = new QuanHuyen(nuow);
                    frm.ItemEdit = obj;
                    if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        obj.TinhThanh = nuow.Query<TinhThanh>().FirstOrDefault(c => c.Oid == frm.TinhThanhID);
                        nuow.CommitChanges();
                        xpCollectionQuanHuyen.Reload();
                    }
                }
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnSua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Edit();
        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (gvQuanHuyen.GetFocusedRow() != null)
                {
                    if (CommonConfirm("Bạn muốn xóa tỉnh thành này?", "Lưu ý!"))
                    {
                        using (NestedUnitOfWork nuow = xpCollectionQuanHuyen.Session.BeginNestedUnitOfWork())
                        {
                            var obj = nuow.GetNestedObject(xpCollectionQuanHuyen[gvQuanHuyen.GetDataSourceRowIndex(gvQuanHuyen.FocusedRowHandle)]);
                            nuow.Delete(obj);
                            nuow.CommitChanges();
                        }
                        xpCollectionQuanHuyen.Reload();
                    }
                }
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }
    }
}
